package com.sheep.jucdemo.part11.part11_6;

import java.util.*;

/**
 * @Date 2022/5/26 23:19
 * @Created by sheep
 */
public class Test11_6_1 {

    // （1）不同的appKey注册不同的服务
    static Map<Integer, StrategyService> serviceMap = new HashMap<Integer,StrategyService>();
    static {
        serviceMap.put(111, new StrategyOneService());
        serviceMap.put(222, new StrategyTwoService());
    }

    public static void main(String[] args) {
        // （2）key为appKey，value为设备ID列表
        Map<Integer, List<String>> appKeyMap = new HashMap<>();

        // （3）创建appKey=111的设备列表
        List<String> oneList = new ArrayList<>();
        oneList.add("device_id1");
        appKeyMap.put(111,oneList);

        // 创建appKey=222的设备列表
        List<String> twoList = new ArrayList<>();
        twoList.add("device_id2");
        appKeyMap.put(222,twoList);

        // （4）创建消息
        List<Msg> msgList = new ArrayList<>();
        Msg msg = new Msg("abc", "hello");
        msgList.add(msg);
        // （5）给每个appKey准备自己的消息列表
        Iterator<Integer> appKeyItr = appKeyMap.keySet().iterator();
        HashMap<Integer, List<Msg>> appKeyMsgMap = new HashMap<>();
        while (appKeyItr.hasNext()){
            appKeyMsgMap.put(appKeyItr.next(), new ArrayList<>(msgList));
        }
        appKeyItr = appKeyMap.keySet().iterator();
        while (appKeyItr.hasNext()){
            int appKey = appKeyItr.next();
            // 根据appKey获取自己的消息列表
            StrategyService strategyService = serviceMap.get(appKey);
            if (null != strategyService){
                strategyService.sendMsg(appKeyMsgMap.get(appKey), appKeyMap.get(appKey));
            }else {
                System.out.println(String.format("appKey:%s, is not registerd service", appKey));
            }
        }
    }

}
